﻿using BPMS.Business;
using BPMS.Entity;
using BPMS.IBusiness;
using DotNet.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace BPMS.MVC.Controllers
{
    public class RolePermissionController : BaseController
    {
        BPMS_RoleMenuIBLL bpms_rolemenuibll = new BPMS_RoleMenuBLL();
        BPMS_OrganizationIBLL bpms_organizationibll = new BPMS_OrganizationBLL();
        BPMS_UserRoleIBLL bpms_userroleibll = new BPMS_UserRoleBLL();
        BPMS_RolesIBLL bpms_rolesibll = new BPMS_RolesBLL();
        BPMS_RoleMenuButtonIBLL bpms_rolemenubuttonibll = new BPMS_RoleMenuButtonBLL();

        #region 角色权限管理
        /// <summary>
        /// 角色权限管理
        /// </summary>
        /// <returns></returns>
        public ActionResult RoleIndexPermission()
        {
            return View();
        }
        #endregion

        #region 角色权限
        /// <summary>
        /// 角色权限
        /// </summary>
        /// <returns></returns>
        public ActionResult RolePermission()
        {
            return View();
        }
        /// <summary>
        /// 处理数据
        /// </summary>
        public void RolePermissionLoadAction()
        {
            string active = Request["action"];                                          //提交类型
            string OrganizationId = Request["OrganizationId"];                          //公司主键
            string RoleId = Request["RoleId"];                                          //角色主键
            switch (active)
            {
                case "InitRoles"://加载角色列表
                    Response.Write(JsonHelper.ListToJson<BPMS_Roles>(bpms_rolesibll.GetList(OrganizationId), "JSON"));
                    Response.End();
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region 角色权限-公司
        /// <summary>
        /// 角色权限-公司
        /// </summary>
        /// <returns></returns>
        public ActionResult CompanyTreePermission()
        {
            ViewData["strHtml"] = GetOrgTree();
            return View();
        }
        /// <summary>
        /// 组织机构
        /// </summary>
        public string GetOrgTree()
        {
            StringBuilder strHtml = new StringBuilder();
            IList list = bpms_organizationibll.GetList();
            List<BPMS_Organization> itemNode = IListHelper.IListToList<BPMS_Organization>(list).FindAll(t => t.ParentId == "0");
            foreach (BPMS_Organization entity in itemNode)
            {
                strHtml.Append("<li>");
                strHtml.Append("<div id='" + entity.OrganizationId + "'><img src='/Themes/images/16/house.png' style='vertical-align: middle;' alt=''/><span>" + entity.FullName + "</span></div>");
                //创建子节点
                strHtml.Append(GetOrgTreeNode(list,entity.OrganizationId));
                strHtml.Append("</li>");
            }
            return strHtml.ToString();
        }
        /// <summary>
        /// 创建子节点
        /// </summary>
        /// <param name="list">list</param>
        /// <returns></returns>
        public string GetOrgTreeNode(IList list,string ParentId)
        {
            StringBuilder sb_TreeNode = new StringBuilder();
            List<BPMS_Organization> itemNode = IListHelper.IListToList<BPMS_Organization>(list).FindAll(t => t.Category == "公司");
            int index = 0;
            string strclass = "";
            if (itemNode.Count > 0)
            {
                sb_TreeNode.Append("<ul>");
                foreach (BPMS_Organization entity in itemNode)
                {
                    if (index == 0)
                        strclass = "collapsableselected";
                    else
                        strclass = "";
                    if (entity.ParentId == ParentId)
                    {
                        sb_TreeNode.Append("<li>");
                        sb_TreeNode.Append("<div class='" + strclass + "' id='" + entity.OrganizationId + "'><img src='/Themes/images/16/chart_organisation.png' style='vertical-align: middle;' alt=''/><span>" + entity.FullName + "</span></div>");
                        sb_TreeNode.Append("</li>");   
                    }
                    index++;
                }
                sb_TreeNode.Append("</ul>");
            }
            return sb_TreeNode.ToString();
        }
        #endregion

        #region 角色成员列表
        /// <summary>
        /// 角色成员列表
        /// </summary>
        /// <returns></returns>
        public ActionResult MemberList()
        {
            string[] ShowButton = { "Replace", "AddMember", "DeleteMember", "Leave" };
            BaseController Strconditio = new BaseController();
            Strconditio.Strconditio = ShowButton;
            return View();
        }
        /// <summary>
        /// 数据处理
        /// </summary>
        public void MemberLoadAction()
        {
            string active = Request["action"];                                          //提交类型
            string orderField = Request["pqGrid_OrderField"];                           //排序字段  
            string orderType = Request["pqGrid_OrderType"];                             //排序类型
            string pqGrid_Sort = Request["pqGrid_Sort"];                                //要显示字段
            string UserId = Request["UserId"];                                          //用户主键
            string RoleId = Request["RoleId"];                                          //角色主键
            string Parm_Key_Value = Request["Parm_Key_Value"];                          //搜索条件键值
            switch (active)
            {
                case "InitRolesMember": //加载角色成员
                    Hashtable parm = HashtableHelper.String_Key_ValueToHashtable(Parm_Key_Value);
                    parm["RoleId"] = RoleId;
                    Response.Write(JsonHelper.PqGridJson<BPMS_User>(bpms_userroleibll.GetListMember(parm), pqGrid_Sort));
                    Response.End();
                    break;
                case "DeleteMember"://删除角色成员
                    string[] array = new string[0];
                    if (!string.IsNullOrEmpty(UserId))
                    {
                        array = UserId.Split(',');
                    }
                    Response.Write(bpms_userroleibll.BatchDeleteMember(RoleId, array));
                    Response.End();
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region 角色成员表单
        StringBuilder sbBranch = new StringBuilder();
        StringBuilder sbMember = new StringBuilder();
        /// <summary>
        /// 角色成员表单
        /// </summary>
        /// <returns></returns>
        public ActionResult MemberForm()
        {
            string _RoleId = Request["RoleId"];                  //角色主键
            string _OrganizationId = Request["OrganizationId"];  //公司主键
            MemberloadBind(_RoleId, _OrganizationId);
            ViewData["strRoleName"] = Server.UrlDecode(Request["RoleName"]);
            ViewData["sbBranch"] = sbBranch.ToString();
            ViewData["sbMember"] = sbMember.ToString();
            return View();
        }
        /// <summary>
        /// 加载部门
        /// </summary>
        /// <param name="_RoleId">角色主键</param>
        /// <param name="_OrganizationId">OrganizationId</param>
        public void MemberloadBind(string _RoleId, string _OrganizationId)
        {
            IList ListNotMember = bpms_userroleibll.GetListNotMember(_RoleId);
            IList list = bpms_organizationibll.GetList();
            List<BPMS_Organization> itemNode = IListHelper.IListToList<BPMS_Organization>(list).FindAll(t => t.ParentId == _OrganizationId);
            int index = 0;
            string strclass = "";
            foreach (BPMS_Organization item in itemNode)
            {
                if (index == 0)
                {
                    strclass = "collapsableselected";
                }
                else
                {
                    strclass = "";
                }
                sbBranch.Append("<li><div class='" + strclass + "' id='" + item.OrganizationId + "'><img src='/Themes/images/16/wand.png' style='vertical-align: middle;' alt=''/>" + item.FullName + "</div></li>");
                MemberNodeMember(ListNotMember, item.OrganizationId);
                index++;
            }
        }
        //加载部门成员
        public void MemberNodeMember(IList ListNotMember, string DepartmentId)
        {
            List<BPMS_User> itemNode = IListHelper.IListToList<BPMS_User>(ListNotMember).FindAll(t => t.DepartmentId == DepartmentId);
            sbMember.Append("<div id='GroupsMember_" + DepartmentId + "' class='DepartmentMember'  style='display:none;'>");
            if (itemNode.Count > 0)
            {

                foreach (BPMS_User item in itemNode)
                {
                    string Genderimg = "user_red.png";
                    string Gender = item.Gender;
                    if (Gender == "男")
                    {
                        Genderimg = "user_mature.png";
                    }
                    sbMember.Append("<div title='编号：" + item.Code + "\r\n账户：" + item.Account + "' class=\"checkbuttonNo panelcheck\">");
                    sbMember.Append("<div id=\"" + item.UserId + "\" class=\"checktext\">");
                    sbMember.Append("<img src=\"../../Themes/Images/16/" + Genderimg + "\" />" + item.RealName + "");
                    sbMember.Append("</div>");
                    sbMember.Append("<div class=\"triangleNo\"></div>");
                    sbMember.Append("</div>");
                }
            }
            else
            {
                sbMember.Append(" <span style='color:red;'>暂无成员</span>");
            }
            sbMember.Append("</div>");
        }
        /// <summary>
        /// 确认事件
        /// </summary>
        /// <param name="UserId">用户</param>
        /// <param name="RoleId">角色</param>
        /// <returns></returns>
        public ActionResult MemberAcceptClick(string UserId, string RoleId)
        {
            string[] array = new string[0];
            if (!string.IsNullOrEmpty(UserId))
            {
                array = UserId.Split('≌');
            }
            bool IsOk = false;
            string CreateUserId = SessionHelper.GetSessionUser().UserId;
            string CreateUserName = SessionHelper.GetSessionUser().UserName;
            IsOk = bpms_userroleibll.BatchAddMember(array, RoleId, CreateUserId, CreateUserName);
            return Content(IsOk.ToString().ToLower());
        }
        #endregion

        #region 角色模块权限
        /// <summary>
        /// 角色模块权限
        /// </summary>
        /// <returns></returns>
        public ActionResult ModulePermission()
        {
            string _RoleId = Request["RoleId"];//角色主键
            ViewData["TableTree_Menu"] = GetMenuTreeTable(_RoleId);
            string[] ShowButton = { "Replace", "AcceptReset", "Authorization", "Leave" };
            BaseController Strconditio = new BaseController();
            Strconditio.Strconditio = ShowButton;
            return View();
        }
        /// <summary>
        /// 模块菜单列表
        /// </summary>
        /// <param name="_RoleId">角色</param>
        /// <returns></returns>
        public string GetMenuTreeTable(string _RoleId)
        {
            StringBuilder TableTree_Menu = new StringBuilder();
            DataTable dt = bpms_rolemenuibll.GetList(_RoleId);
            DataView dv = new DataView(dt);
            dv.RowFilter = " ParentId = '0'";
            int eRowIndex = 0;
            foreach (DataRowView drv in dv)
            {
                string trID = "node-" + eRowIndex.ToString();
                TableTree_Menu.Append("<tr id='" + trID + "'>");
                TableTree_Menu.Append("<td style='width: 230px;padding-left:20px;'><input id='ckb" + trID + "' onclick=\"ckbValueObj(this.id)\" " + GetChecked(drv["IsExist"].ToString()) + " style='vertical-align: middle;margin-bottom:2px;' type=\"checkbox\" value=\"" + drv["MenuId"] + "\" name=\"checkbox\" /><img src='/Themes/images/32/" + drv["Img"] + "' style='width:20px; height:20px;vertical-align: middle;' alt=''/><span style='padding-left:8px;'>" + drv["FullName"] + "</span></td>");
                TableTree_Menu.Append("<td style='width: 100px;text-align: center;'>" + drv["Code"] + "</td>");
                TableTree_Menu.Append("<td style='width: 60px;text-align:center;'>" + drv["Category"] + "</td>");
                TableTree_Menu.Append("<td style='width: 60px;text-align:center;'>" + drv["SortCode"] + "</td>");
                TableTree_Menu.Append("<td>" + drv["Description"] + "</td>");
                TableTree_Menu.Append("</tr>");
                //创建子节点
                TableTree_Menu.Append(GetTableTreeNode(drv["MenuId"].ToString(), dt, trID));
                eRowIndex++;
            }
            return TableTree_Menu.ToString();
        }
        /// <summary>
        /// 创建子节点
        /// </summary>
        /// <param name="parentID">父节点主键</param>
        /// <param name="list">菜单集合</param>
        /// <returns></returns>
        public string GetTableTreeNode(string parentID, DataTable dt, string parentTRID)
        {
            StringBuilder sb_TreeNode = new StringBuilder();
            int i = 1;
            DataView dv = new DataView(dt);
            dv.RowFilter = "ParentId = '" + parentID + "'";
            foreach (DataRowView drv in dv)
            {
                string trID = parentTRID + "-" + i.ToString();
                sb_TreeNode.Append("<tr id='" + trID + "' class='child-of-" + parentTRID + "'>");
                sb_TreeNode.Append("<td style='padding-left:20px;'><input id='ckb" + trID + "' onclick=\"ckbValueObj(this.id)\" " + GetChecked(drv["IsExist"].ToString()) + " style='vertical-align: middle;margin-bottom:2px;' type=\"checkbox\" value=\"" + drv["MenuId"] + "\" name=\"checkbox\" /><img src='/Themes/images/32/" + drv["Img"] + "' style='width:20px; height:20px;vertical-align: middle;' alt=''/><span style='padding-left:8px;'>" + drv["FullName"] + "</span></td>");
                sb_TreeNode.Append("<td style='width: 100px;text-align: center;'>" + drv["Code"] + "</td>");
                sb_TreeNode.Append("<td style='width: 60px;text-align:center;'>" + drv["Category"] + "</td>");
                sb_TreeNode.Append("<td style='width: 60px;text-align:center;'>" + drv["SortCode"] + "</td>");
                sb_TreeNode.Append("<td>" + drv["Description"] + "</td>");
                sb_TreeNode.Append("</tr>");
                //创建子节点
                sb_TreeNode.Append(GetTableTreeNode(drv["MenuId"].ToString(), dt, trID));
                i++;
            }
            return sb_TreeNode.ToString();
        }
        /// <summary>
        /// 验证权限是否存在
        /// </summary>
        /// <param name="IsExist"></param>
        /// <returns></returns>
        public string GetChecked(string IsExist)
        {
            if (!string.IsNullOrEmpty(IsExist))
                return "checked=\"checked\"";
            else
                return "";
        }
        /// <summary>
        /// 分配角色模块权限
        /// </summary>
        /// <param name="RoleId">角色主键</param>
        /// <param name="MenuId">模块菜单主键</param>
        /// <returns></returns>
        public ActionResult AddModulePermission(string RoleId, string MenuId)
        {
            string[] array = new string[0];
            if (!string.IsNullOrEmpty(MenuId))
            {
                array = MenuId.Split(',');
            }
            string CreateUserId = SessionHelper.GetSessionUser().UserId;
            string CreateUserName = SessionHelper.GetSessionUser().UserName;
            CacheHelper.RemoveAllCache();
            bool IsOk = bpms_rolemenuibll.AddModulePermission(array, RoleId, CreateUserId, CreateUserName);
            return Content(IsOk.ToString().ToLower());
        }
        #endregion

        #region 角色按钮权限
        StringBuilder sbModule = new StringBuilder();
        StringBuilder sbButton = new StringBuilder();
        /// <summary>
        /// 角色按钮权限
        /// </summary>
        /// <returns></returns>
        public ActionResult ButtonPermission()
        {
            string _RoleId = Request["RoleId"];//角色主键
            GetMenuTreeList(_RoleId);
            ViewData["sbModule"] = sbModule.ToString();
            ViewData["sbButton"] = sbButton.ToString();
            string[] ShowButton = { "Replace", "AcceptReset", "Authorization", "Leave" };
            BaseController Strconditio = new BaseController();
            Strconditio.Strconditio = ShowButton;
            return View();
        }
        /// <summary>
        /// 模块菜单树
        /// </summary>
        /// <param name="_RoleId"></param>
        public void GetMenuTreeList(string _RoleId)
        {
            DataTable dtButton = bpms_rolemenubuttonibll.GetList(_RoleId);
            DataTable list = bpms_rolemenuibll.GetList(_RoleId);
            DataView dv = new DataView(list);
            dv.RowFilter = " ParentId = '0'";
            foreach (DataRowView drv in dv)
            {
                if (!string.IsNullOrEmpty(drv["IsExist"].ToString()))
                {
                    sbModule.Append("<li>");
                    sbModule.Append("<div id='" + drv["MenuId"] + "'><img src='/Themes/images/32/" + drv["Img"] + "' style='width:18px; height:18px;vertical-align: middle;' alt=''/>" + drv["FullName"] + "</div>");
                    //创建子节点
                    sbModule.Append(GetTreeNode(drv["MenuId"].ToString(), list, dtButton));
                    NodeButton(dtButton, drv["MenuId"].ToString());
                    sbModule.Append("</li>");
                }
            }
        }
        /// <summary>
        /// 创建子节点
        /// </summary>
        /// <param name="parentID">父节点主键</param>
        /// <param name="list">菜单集合</param>
        /// <returns></returns>
        public string GetTreeNode(string ParentId, DataTable list, DataTable dtButton)
        {
            StringBuilder sb_TreeNode = new StringBuilder();
            DataView dv = new DataView(list);
            dv.RowFilter = "ParentId = '" + ParentId + "'";
            if (dv.Count > 0)
            {
                sb_TreeNode.Append("<ul>");
                foreach (DataRowView drv in dv)
                {
                    if (!string.IsNullOrEmpty(drv["IsExist"].ToString()))
                    {
                        sb_TreeNode.Append("<li>");
                        sb_TreeNode.Append("<div id='" + drv["MenuId"] + "'><img src='/Themes/images/32/" + drv["Img"] + "' style='width:18px; height:18px;vertical-align: middle;' alt=''/>" + drv["FullName"] + "</div>");
                        //创建子节点
                        sb_TreeNode.Append(GetTreeNode(drv["MenuId"].ToString(), list, dtButton));
                        NodeButton(dtButton, drv["MenuId"].ToString());
                        sb_TreeNode.Append("</li>");
                    }
                }
                sb_TreeNode.Append("</ul>");
            }
            return sb_TreeNode.ToString();
        }
        /// <summary>
        /// 加载模块菜单按钮
        /// </summary>
        /// <param name="dtButton">按钮集合</param>
        /// <param name="MenuId">模块主键</param>
        public void NodeButton(DataTable dtButton, string MenuId)
        {
            sbButton.Append("<div id='Module_" + MenuId + "' class='ModuleButton'  style='display:none;'>");
            DataView dv = new DataView(dtButton);
            dv.RowFilter = " MenuId = '" + MenuId + "'";
            if (dv.Count > 0)
            {
                sbButton.Append("<div class=\"bd\" style='border-left: 0px solid #ccc;border-right: 0px solid #ccc;border-bottom: 0px solid #ccc;'><div class=\"tipstitle_title settingtable bold bd todayInfoPanelTab rightPanelTitle_normal\">");
                sbButton.Append("<div style=\"position: absolute\">");
                sbButton.Append("<div id=\"tb_toolbar_" + MenuId + "\" class=\"tab_list bd actived\" onclick=\"$('#tb_Application_" + MenuId + "').removeClass('actived');;$(this).addClass('actived');$('#div_toolbar_" + MenuId + "').show();$('#div_Application" + MenuId + "').hide();\">工具栏</div>");
                sbButton.Append("<div id=\"tb_Application_" + MenuId + "\" class=\"tab_list bd\" onclick=\"$('#tb_toolbar_" + MenuId + "').removeClass('actived');$(this).addClass('actived');$('#div_Application" + MenuId + "').show();$('#div_toolbar_" + MenuId + "').hide();\">右击菜单栏</div>");
                sbButton.Append("</div>");
                sbButton.Append("</div>");
                sbButton.Append("<div class=\"ScrollBar\" style=\"padding: 5px;\">");
                StringBuilder sbtoolbar = new StringBuilder();
                StringBuilder sbApplication = new StringBuilder();
                foreach (DataRowView drv in dv)
                {
                    string checkbuttonNo = "checkbuttonNo";
                    string triangleNo = "triangleNo";
                    string IsExist = drv["IsExist"].ToString();
                    if (!string.IsNullOrEmpty(IsExist))
                    {
                        checkbuttonNo = "checkbuttonOk";
                        triangleNo = "triangleOk";
                    }

                    if (drv["Category"].ToString() == "工具栏")
                    {
                        sbtoolbar.Append("<div class=\"" + checkbuttonNo + " panelcheck\">");
                        sbtoolbar.Append("<div id=\"" + drv["ButtonId"] + "|" + drv["MenuId"] + "\" class=\"checktext\">");
                        sbtoolbar.Append("<img src=\"../../Themes/Images/16/" + drv["Img"] + "\" />" + drv["FullName"] + "");
                        sbtoolbar.Append("</div>");
                        sbtoolbar.Append("<div class=\"" + triangleNo + "\"></div>");
                        sbtoolbar.Append("</div>");
                    }
                    else
                    {
                        if (drv["Category"].ToString() == "右击菜单栏")
                        {
                            sbApplication.Append("<div class=\"" + checkbuttonNo + " panelcheck\">");
                            sbApplication.Append("<div id=\"" + drv["ButtonId"] + "|" + drv["MenuId"] + "\" class=\"checktext\">");
                            sbApplication.Append("<img src=\"../../Themes/Images/16/" + drv["Img"] + "\" />" + drv["FullName"] + "");
                            sbApplication.Append("</div>");
                            sbApplication.Append("<div class=\"" + triangleNo + "\"></div>");
                            sbApplication.Append("</div>");
                        }
                    }
                }
                sbButton.Append("<div id=\"div_toolbar_" + MenuId + "\">");
                sbButton.Append(sbtoolbar);
                sbButton.Append("</div>");
                sbButton.Append("<div id=\"div_Application" + MenuId + "\" style='display:none'>");
                sbButton.Append(sbApplication);
                sbButton.Append("</div>");
                sbButton.Append("</div>");
                sbButton.Append("</div>");
            }
            else
            {
                sbButton.Append(" <span style='color:red;'>暂无按钮</span>");
            }
            sbButton.Append("</div>");
        }

        /// <summary>
        /// 分配角色按钮权限
        /// </summary>
        /// <param name="RoleId">角色主键</param>
        /// <param name="ButtonId">按钮主键</param>
        /// <returns></returns>
        public ActionResult AddButtonPermission(string RoleId, string ButtonId)
        {
            string[] array = new string[0];
            if (!string.IsNullOrEmpty(ButtonId))
            {
                array = ButtonId.Split('≌');
            }
            string CreateUserId = SessionHelper.GetSessionUser().UserId;
            string CreateUserName = SessionHelper.GetSessionUser().UserName;
            CacheHelper.RemoveAllCache();
            bool IsOk = bpms_rolemenubuttonibll.AddButtonPermission(array, RoleId, CreateUserId, CreateUserName);
            return Content(IsOk.ToString().ToLower());
        }
        #endregion

        #region 角色数据权限管理
        BPMS_DataPermissionIBLL bpms_datapermissionibll = new BPMS_DataPermissionBLL();
        BPMS_DataPermission entity = new BPMS_DataPermission();
        /// <summary>
        /// 角色数据权限管理
        /// </summary>
        /// <returns></returns>
        public ActionResult DataPermissionList()
        {
            string[] ShowButton = { "Replace", "AcceptReset", "Authorization", "Leave" };
            BaseController Strconditio = new BaseController();
            Strconditio.Strconditio = ShowButton;
            return View();
        }

        #region 组织机构数据权限列表
        /// <summary>
        /// 组织机构
        /// </summary>
        public string GetOrgTree(string RoleId, string AuthorizationItemValue)
        {
            BPMS_DataPermission datapermission = bpms_datapermissionibll.GetEntity(RoleId, AuthorizationItemValue);
            string[] array = new string[0];
            if (!string.IsNullOrEmpty(datapermission.ObjectId))
            {
                array = datapermission.ObjectId.Replace("'", "").Split(',');
            }
            StringBuilder strHtml = new StringBuilder();
            IList list = bpms_datapermissionibll.GetOrganizationList();
            List<BPMS_Organization> itemNode = IListHelper.IListToList<BPMS_Organization>(list).FindAll(t => t.ParentId == "0");
            foreach (BPMS_Organization entity in itemNode)
            {
                strHtml.Append("<li>");
                bool exists = ((IList)array).Contains(entity.OrganizationId);
                strHtml.Append("<input " + GetChecked(exists) + " style='vertical-align: middle;margin-bottom:2px;' type=\"checkbox\" value=\"" + entity.OrganizationId + "\" name=\"checkbox\" /><img src='/Themes/images/16/house.png' style='vertical-align: middle;' alt=''/>" + entity.FullName + "");
                //创建子节点
                strHtml.Append(GeOrgtTreeNode(entity.OrganizationId, list, array));
                strHtml.Append("</li>");
            }
            return strHtml.ToString();
        }
        /// <summary>
        /// 创建子节点
        /// </summary>
        /// <param name="parentID">父节点主键</param>
        /// <param name="list">菜单集合</param>
        /// <returns></returns>
        public string GeOrgtTreeNode(string ParentId, IList list, string[] array)
        {
            StringBuilder sb_TreeNode = new StringBuilder();
            List<BPMS_Organization> itemNode = IListHelper.IListToList<BPMS_Organization>(list).FindAll(t => t.ParentId == ParentId);
            if (itemNode.Count > 0)
            {
                sb_TreeNode.Append("<ul>");
                foreach (BPMS_Organization entity in itemNode)
                {

                    bool exists = ((IList)array).Contains(entity.OrganizationId);
                    sb_TreeNode.Append("<li title='" + entity.Category + "'>");
                    sb_TreeNode.Append("<input " + GetChecked(exists) + " style='vertical-align: middle;margin-bottom:2px;' type=\"checkbox\" value=\"" + entity.OrganizationId + "\" name=\"checkbox\" /><img src='/Themes/images/16/" + CategoryToImg(entity.Category) + "' style='vertical-align: middle;' alt=''/>" + entity.FullName + "");
                    //创建子节点
                    sb_TreeNode.Append(GeOrgtTreeNode(entity.OrganizationId, list, array));
                    sb_TreeNode.Append("</li>");
                }
                sb_TreeNode.Append("</ul>");
            }
            return sb_TreeNode.ToString();
        }
        public string CategoryToImg(string Category)
        {
            string img = "";
            if (Category == "区域")
            {
                img = "house_star.png";
            }
            if (Category == "公司")
            {
                img = "chart_organisation.png";
            }
            if (Category == "子公司")
            {
                img = "flag_green.png";
            }
            if (Category == "部门")
            {
                img = "wand.png";
            }
            if (Category == "工作组")
            {
                img = "users.png";
            }
            return img;
        }
        #endregion

        #region 用户列表数据权限列表
        /// <summary>
        /// 用户列表
        /// </summary>
        /// <returns></returns>
        public string GetUserList(string RoleId, string AuthorizationItemValue)
        {
            BPMS_DataPermission datapermission = bpms_datapermissionibll.GetEntity(RoleId, AuthorizationItemValue);
            string[] array = new string[0];
            if (!string.IsNullOrEmpty(datapermission.ObjectId))
            {
                array = datapermission.ObjectId.Replace("'", "").Split(',');
            }

            StringBuilder strHtml = new StringBuilder();
            IList list = bpms_datapermissionibll.GetUserList();
            strHtml.Append("<li>");
            strHtml.Append("<input style='vertical-align: middle;margin-bottom:2px;' type=\"checkbox\" name=\"checkbox\" /><img src='/Themes/images/16/spellcheck.png' style='vertical-align: middle;' alt=''/>所有用户");
            if (list.Count > 0)
            {
                strHtml.Append("<ul>");
                foreach (BPMS_User entity in list)
                {
                    bool exists = ((IList)array).Contains(entity.UserId);
                    string Genderimg = "user_red.png";
                    var Gender = entity.Gender;
                    if (Gender == "男")
                    {
                        Genderimg = "user_mature.png";
                    }
                    strHtml.Append("<li>");
                    strHtml.Append("<input " + GetChecked(exists) + " style='vertical-align: middle;margin-bottom:2px;' type=\"checkbox\" value=\"" + entity.UserId + "\" name=\"checkbox\" /><img src='/Themes/images/16/" + Genderimg + "' style='vertical-align: middle;' alt=''/>" + entity.Account + " - " + entity.RealName + "");
                    strHtml.Append("</li>");
                }
                strHtml.Append("</ul>");
            }
            else
            {
                strHtml.Append("<li><div><span style='color:red;'>暂无用户</span></div>");
            }
            strHtml.Append("</li>");
            return strHtml.ToString();
        }
        #endregion

        #region 员工列表数据权限列表
        /// <summary>
        /// 员工列表
        /// </summary>
        /// <returns></returns>
        public string GetEmployeeList(string RoleId, string AuthorizationItemValue)
        {
            BPMS_DataPermission datapermission = bpms_datapermissionibll.GetEntity(RoleId, AuthorizationItemValue);
            string[] array = new string[0];
            if (!string.IsNullOrEmpty(datapermission.ObjectId))
            {
                array = datapermission.ObjectId.Replace("'", "").Split(',');
            }

            StringBuilder strHtml = new StringBuilder();
            IList list = bpms_datapermissionibll.GetEmployeeList();
            strHtml.Append("<li>");
            strHtml.Append("<input style='vertical-align: middle;margin-bottom:2px;' type=\"checkbox\" name=\"checkbox\" /><img src='/Themes/images/16/spellcheck.png' style='vertical-align: middle;' alt=''/>所有员工");
            if (list.Count > 0)
            {
                strHtml.Append("<ul>");
                foreach (BPMS_Person entity in list)
                {
                    bool exists = ((IList)array).Contains(entity.PerId);
                    string Genderimg = "user_red.png";
                    var Gender = entity.Sex;
                    if (Gender == "男")
                    {
                        Genderimg = "user_mature.png";
                    }
                    strHtml.Append("<li>");
                    strHtml.Append("<input " + GetChecked(exists) + " style='vertical-align: middle;margin-bottom:2px;' type=\"checkbox\" value=\"" + entity.PerId + "\" name=\"checkbox\" /><img src='/Themes/images/16/" + Genderimg + "' style='vertical-align: middle;' alt=''/>" + entity.RealName + "");
                    strHtml.Append("</li>");
                }
                strHtml.Append("</ul>");
            }
            else
            {
                strHtml.Append("<li><div><span style='color:red;'>暂无员工</span></div>");
            }
            strHtml.Append("</li>");
            return strHtml.ToString();
        }
        #endregion

        /// <summary>
        /// 验证权限是否存在
        /// </summary>
        /// <param name="exists"></param>
        /// <returns></returns>
        public string GetChecked(bool exists)
        {
            if (exists)
                return "checked=\"checked\"";
            else
                return "";
        }
        /// <summary>
        /// 数据处理
        /// </summary>
        public void DataPermissionLoadAction()
        {
            Response.Buffer = true;
            Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
            Response.Expires = 0;
            Response.CacheControl = "no-cache";
            Response.AddHeader("Pragma", "No-Cache");

            string active = Request["action"];                                          //提交类型
            string RoleId = Request["RoleId"];                                          //角色主键
            string AuthorizationItemValue = Request["AuthorizationItemValue"];          //授权项目
            string itemId = Request["itemId"];                                          //授权项目对象Id
            switch (active)
            {
                case "LoadOrgTree"://组织机构
                    Response.Write(GetOrgTree(RoleId, AuthorizationItemValue));
                    Response.End();
                    break;
                case "LoadUserList"://用户列表
                    Response.Write(GetUserList(RoleId, AuthorizationItemValue));
                    Response.End();
                    break;
                case "LoadEmployeeList"://员工列表
                    Response.Write(GetEmployeeList(RoleId, AuthorizationItemValue));
                    Response.End();
                    break;
                case "AddDataPermission"://添加数据权限
                    bool IsOk = false;
                    entity = bpms_datapermissionibll.GetEntity(RoleId, AuthorizationItemValue);
                    if (entity.DataPermissionId == null)
                    {
                        entity.DataPermissionId = CommonHelper.GetGuid;
                        entity.RoleId = RoleId;
                        entity.ResourceId = AuthorizationItemValue;
                        entity.ObjectId = itemId;
                        entity.CreateUserId = SessionHelper.GetSessionUser().UserId;
                        entity.CreateUserName = SessionHelper.GetSessionUser().UserName;
                        IsOk = bpms_datapermissionibll.Insert(entity);
                    }
                    else
                    {
                        entity.RoleId = RoleId;
                        entity.ResourceId = AuthorizationItemValue;
                        entity.ObjectId = itemId;
                        entity.CreateUserId = SessionHelper.GetSessionUser().UserId;
                        entity.CreateUserName = SessionHelper.GetSessionUser().UserName;
                        IsOk = bpms_datapermissionibll.Update(entity);
                    }
                    CacheHelper.RemoveAllCache();
                    Response.Write(IsOk);
                    Response.End();
                    break;
                default:
                    break;
            }
        }
        #endregion
    }
}
